package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.ConformOrderBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Calendar;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ConformOrder extends AppCompatActivity {
    private TextView TVPatientName;
    private TextView TVDepMart;
    private TextView TVHospitalType;
    private TextView TVMoeny;
    private TextView TVSelectDate;
    private TextView TVSelectTime;
    private TextView TVBtnOk;
    private TextView TVCancel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conform_order);
        TVPatientName = findViewById(R.id.TV_patientName);
        TVDepMart = findViewById(R.id.TV_DepMart);
        TVHospitalType = findViewById(R.id.TV_hospitalType);
        TVMoeny = findViewById(R.id.TV_moeny);
        TVSelectDate = findViewById(R.id.TV_selectDate);
        TVSelectTime = findViewById(R.id.TV_selectTime);
        TVCancel = findViewById(R.id.TV_btnCanel);
        TVBtnOk = findViewById(R.id.TV_btnOk);
        initView();
        Order();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        Calendar calendar = Calendar.getInstance();
        TVPatientName.setText(getIntent().getStringExtra("TV_userNName"));
        TVDepMart.setText(getIntent().getStringExtra("DepMart"));
        TVMoeny.setText(getIntent().getStringExtra("money"));
        TVHospitalType.setText(getIntent().getIntExtra("id",1)==1?"专家号":"普通号");
        TVSelectDate.setText(calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DAY_OF_MONTH));
        TVSelectTime.setText(calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE));
        TVSelectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(ConformOrder.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        TVSelectDate.setText(year+"-"+month+"-"+dayOfMonth);
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        TVSelectTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog(ConformOrder.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        TVSelectTime.setText(hourOfDay+":"+minute);
                    }
                },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),false).show();
            }
        });
    }
    public void Order(){
        TVCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConformOrder.this.finish();
            }
        });
        TVBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/order";
                ConformOrderBean bean = new ConformOrderBean();
                bean.setStartime(TVSelectDate.getText().toString()+" "+TVSelectTime.getText().toString());
                bean.setUserId(getSharedPreferences("selfMessage",MODE_PRIVATE).getString("userID",null));
                bean.setTypesId(String.valueOf(getIntent().getIntExtra("id",0)));
                bean.setDivisionId(getIntent().getIntExtra("idH", 0));
                bean.setMoeny(TVMoeny.getText().toString());
                bean.setPatientName(TVPatientName.getText().toString());
                Gson gson= new Gson();
                String Json = gson.toJson(bean);
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),Json);
                OkHttpUtils.getCall(newUrl,requestBody,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("TAG", "onFailure: "+e.getMessage());
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        stateBean stateBean = gson.fromJson(response.body().string(), com.example.smartcity.ui.Bean.stateBean.class);
                        Looper.prepare();
                        if (stateBean.getCode()==200){
                            Toast.makeText(ConformOrder.this, "预约成功", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(ConformOrder.this,OrderHistory.class));
                        }else{
                            Toast.makeText(ConformOrder.this, stateBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        Looper.loop();
                    }
                });
            }
        });
    }

}